package com.excesys.exsecs.eco.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.eco.entity.EcoCoolHourFlow;
import com.excesys.exsecs.eco.entity.EcoPowerHourFlow;
import com.excesys.exsecs.eco.mapper.EcoCoolHourFlowMapper;
import com.excesys.exsecs.eco.mapper.EcoPowerHourFlowMapper;
import com.excesys.exsecs.eco.service.EcoCoolHourFlowService;
import com.excesys.exsecs.eco.util.DateUtil;
import com.excesys.exsecs.eco.util.RandomUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
 * 节能制冷量小时表
 *
 * @author lxc
 * @date 2024-11-26 13:31:09
 */
@Service
@RequiredArgsConstructor
public class EcoCoolHourFlowServiceImpl extends ServiceImpl<EcoCoolHourFlowMapper, EcoCoolHourFlow> implements EcoCoolHourFlowService {

    private final EcoPowerHourFlowMapper ecoPowerHourFlowMapper;

    @Override
    public double getSumTotalCool(String date) {
        return getBaseMapper().getSumTotalCool(date);
    }

    @Override
    public double getSumTotalCop(String date) {
        return getBaseMapper().getSumTotalCop(date);
    }

    @Override
    public List<Map<String, Object>> chartPower(String startTime, String endTime) {
        return getBaseMapper().chartPower(startTime,endTime);
    }

    @Override
    public List<Map<String, Object>> chartPowerCop(String startTime, String endTime) {
        return getBaseMapper().chartPowerCop(startTime,endTime);
    }

    @Override
    public void initPowerHourFlowData() {
        //1.初始化用电量
        /*String[] unitNos = new String[]{"9f6cc14a086b44069ee6059b27033405","6b0addbf05d841bdb0f76a60391ab21a",
                "e6e89d1b69c74caf9ab6dd098e030faf","4e81519cbe3446bda8ec89bdb74bc853","2275100b7dbe46ffa866fc555355ec3f", "4aa648e87fe24412b8d3b2b6f636e406",
                "b401ca52fec74177b40dc7a8f5674bac","900d760d1a7a4ab296367a362103ea56","b6b6823d4bf842eea7aa122f14f4c795","fce19b86fe194b0f8a77af30857dd4dd",
                "d999f4b473d24412a5aba6cac51f2d11","0e44e2fa08bf4101877efa7d90042f32","4bd40b35dd3f42ada10764926c9161ce","72b1b5bc798345b38ae1a483b75d1cc0",
                "6e65e6313a6c4b67aaf2ad0b8d271bc9","29665768d24e4654918670ff2f18c44f","3647402ea2e34d898ee3742063f975fa"};
        for (String unitNo : unitNos) {
            Float initValue = RandomUtil.getFloatRandom(19000, 21000);
            LocalDateTime initDateTime = LocalDateTime.of(2023, 11, 25, 0, 0, 0);
            EcoPowerHourFlow powerHourFlow = new EcoPowerHourFlow(null, unitNo, null,
                    initDateTime, Integer.parseInt(initDateTime.format(DateTimeFormatter.ofPattern("yyyyMM"))), initValue, initValue, initValue,
                    "system", LocalDateTime.now(), null, null);
            ecoPowerHourFlowMapper.insert(powerHourFlow);
        }*/

        String[] zhuji = new String[]{"9f6cc14a086b44069ee6059b27033405","6b0addbf05d841bdb0f76a60391ab21a"};
        String[] lengquebeng = new String[]{"e6e89d1b69c74caf9ab6dd098e030faf","4e81519cbe3446bda8ec89bdb74bc853"};
        String[] lengdongbeng = new String[]{"2275100b7dbe46ffa866fc555355ec3f", "4aa648e87fe24412b8d3b2b6f636e406","b401ca52fec74177b40dc7a8f5674bac","900d760d1a7a4ab296367a362103ea56","b6b6823d4bf842eea7aa122f14f4c795"};
        String[] lengqueta = new String[]{"fce19b86fe194b0f8a77af30857dd4dd","d999f4b473d24412a5aba6cac51f2d11","0e44e2fa08bf4101877efa7d90042f32",
                "4bd40b35dd3f42ada10764926c9161ce","72b1b5bc798345b38ae1a483b75d1cc0","6e65e6313a6c4b67aaf2ad0b8d271bc9","29665768d24e4654918670ff2f18c44f","3647402ea2e34d898ee3742063f975fa"};
        String[] lengdongbeng2 = new String[]{"2275100b7dbe46ffa866fc555355ec3f","b401ca52fec74177b40dc7a8f5674bac"};
        String[] zhuji1 = new String[]{"9f6cc14a086b44069ee6059b27033405"};
        String[] lengquebeng1 = new String[]{"e6e89d1b69c74caf9ab6dd098e030faf"};
        String[] lengqueta1 = new String[]{"fce19b86fe194b0f8a77af30857dd4dd","d999f4b473d24412a5aba6cac51f2d11","0e44e2fa08bf4101877efa7d90042f32","4bd40b35dd3f42ada10764926c9161ce"};


        //2.5月份的用电量
        /*List<LocalDateTime> mays = DateUtil.getDateBetween(LocalDateTime.of(2024, 5, 20, 0, 0), LocalDateTime.of(2024, 5, 31, 23, 0));
        mays.forEach(may ->{
            boolean holidayCheck = DateUtil.HolidayCheck(may);
            if (holidayCheck){
                insertFlowTable(may, zhuji1, 250, 280, 75, 90);
                insertFlowTable(may, lengdongbeng2, 32, 35, 11, 12);
            } else{
                insertFlowTable(may, zhuji, 330, 340, 110, 115);
                insertFlowTable(may, lengdongbeng, 32, 35, 11, 12);
            }
            insertFlowTable(may, lengquebeng, 70, 80, 30, 40);
            insertFlowTable(may, lengqueta, 13, 16, 6, 8);
        });*/

        //6月份的用电量
        /*List<LocalDateTime> junes = DateUtil.getDateBetween(LocalDateTime.of(2024, 6, 1, 0, 0), LocalDateTime.of(2024, 6, 30, 23, 0));
        junes.forEach(june ->{
            boolean holidayCheck = DateUtil.HolidayCheck(june);
            if (holidayCheck){
                insertFlowTable(june, zhuji1, 250, 280, 75, 90);
                insertFlowTable(june, lengdongbeng2, 32, 35, 11, 12);
            } else{
                insertFlowTable(june, zhuji, 330, 340, 110, 115);
                insertFlowTable(june, lengdongbeng, 32, 35, 11, 12);
            }
            insertFlowTable(june, lengquebeng, 70, 80, 30, 40);
            insertFlowTable(june, lengqueta, 13, 16, 6, 8);
        });*/


        //7月份的用电量
        /*List<LocalDateTime> julys = DateUtil.getDateBetween(LocalDateTime.of(2024, 7, 1, 0, 0), LocalDateTime.of(2024, 7, 31, 23, 0));
        julys.forEach(july ->{
            boolean holidayCheck = DateUtil.HolidayCheck(july);
            if (holidayCheck){
                insertFlowTable(july, zhuji1, 360, 400, 120, 150);
                insertFlowTable(july, lengdongbeng2, 45, 55, 15, 20);
            } else{
                insertFlowTable(july, zhuji, 460, 480, 160, 180);
                insertFlowTable(july, lengdongbeng, 45, 55, 15, 20);
            }
            insertFlowTable(july, lengquebeng, 120, 130, 40, 45);
            insertFlowTable(july, lengqueta, 20, 25, 8, 10);
        });*/


        //8月份的用电量
        /*List<LocalDateTime> augusts = DateUtil.getDateBetween(LocalDateTime.of(2024, 8, 1, 0, 0), LocalDateTime.of(2024, 8, 31, 23, 0));
        augusts.forEach(august ->{
            boolean holidayCheck = DateUtil.HolidayCheck(august);
            if (holidayCheck){
                insertFlowTable(august, zhuji1, 340, 390, 110, 140);
                insertFlowTable(august, lengdongbeng2, 36, 39, 12, 13);
            } else{
                insertFlowTable(august, zhuji, 440, 470, 150, 170);
                insertFlowTable(august, lengdongbeng, 36, 39, 12, 13);
            }
            insertFlowTable(august, lengquebeng, 90, 100, 30, 35);
            insertFlowTable(august, lengqueta, 16, 20, 5, 7);
        });*/

        //9月份的用电量
        /*List<LocalDateTime> seps = DateUtil.getDateBetween(LocalDateTime.of(2024, 9, 1, 0, 0), LocalDateTime.of(2024, 9, 30, 23, 0));
        seps.forEach(sep ->{
            boolean holidayCheck = DateUtil.HolidayCheck(sep);
            if (holidayCheck){
                insertFlowTable(sep, zhuji1, 280, 300, 90, 100);
                insertFlowTable(sep, lengdongbeng2, 28, 32, 8, 10);
            } else {
                insertFlowTable(sep, zhuji, 380, 400, 125, 135);
                insertFlowTable(sep, lengdongbeng, 28, 32, 8, 10);
            }
            insertFlowTable(sep, lengquebeng, 70, 75, 23, 25);
            insertFlowTable(sep, lengqueta, 12, 14, 4, 5);
        });*/


        //10月份的用电量
        /*List<LocalDateTime> octs = DateUtil.getDateBetween(LocalDateTime.of(2024, 10, 1, 0, 0), LocalDateTime.of(2024, 10, 31, 23, 0));
        octs.forEach(oct ->{
            boolean holidayCheck = DateUtil.HolidayCheck(oct);
            if (holidayCheck){
                insertFlowTable(oct, zhuji1, 200, 220, 65, 90);
                insertFlowTable(oct, lengdongbeng2, 24, 26, 7, 9);
            } else{
                insertFlowTable(oct, zhuji1, 300, 330, 90, 110);
                insertFlowTable(oct, lengdongbeng2, 24, 26, 7, 9);
            }
            insertFlowTable(oct, lengquebeng1, 65, 70, 21, 23);
            insertFlowTable(oct, lengqueta1, 9, 11, 3, 4);
        });*/

        //11月份的用电量
        /*List<LocalDateTime> novs = DateUtil.getDateBetween(LocalDateTime.of(2024, 11, 16, 0, 0), LocalDateTime.of(2024, 11, 29, 21, 0));
        novs.forEach(nov ->{
            insertFlowTable2(nov, zhuji);
            insertFlowTable2(nov, lengquebeng);
            insertFlowTable(nov, lengdongbeng2, 35, 40, 10, 15);
            insertFlowTable2(nov, lengqueta);
        });*/
    }

    private void insertFlowTable(LocalDateTime dateBetween, String[] arr, Integer highV1, Integer highV2, Integer lowV1, Integer lowV2) {
            for (String unitNo : arr) {
                //主机用电量
                EcoPowerHourFlow pflow = ecoPowerHourFlowMapper.selectOne(Wrappers.<EcoPowerHourFlow>lambdaQuery().eq(EcoPowerHourFlow::getUnitNo, unitNo)
                        .orderByDesc(EcoPowerHourFlow::getTimeValue).last("limit 1"));
                Float totalValue = pflow.getTotalValue();
                Float incrValue = 0f;
                //判断时间段是否在8点-21点之间
                if (dateBetween.getHour() >=8 && dateBetween.getHour() <21) {
                    //耗电量高的时候
                    incrValue = RandomUtil.getFloatRandom(highV1, highV2);
                }else{
                    incrValue = RandomUtil.getFloatRandom(lowV1, lowV2);
                }
                totalValue += incrValue;
                EcoPowerHourFlow powerHourFlow = new EcoPowerHourFlow(null, unitNo, null,
                        dateBetween, Integer.parseInt(dateBetween.format(DateTimeFormatter.ofPattern("yyyyMM"))), totalValue, totalValue, incrValue,
                        "system", LocalDateTime.now(), null, null);
                ecoPowerHourFlowMapper.insert(powerHourFlow);
            }
    }

    private void insertFlowTable2(LocalDateTime dateBetween, String[] arr) {
        for (String unitNo : arr) {
            //主机用电量
            EcoPowerHourFlow pflow = ecoPowerHourFlowMapper.selectOne(Wrappers.<EcoPowerHourFlow>lambdaQuery().eq(EcoPowerHourFlow::getUnitNo, unitNo)
                    .orderByDesc(EcoPowerHourFlow::getTimeValue).last("limit 1"));
            Float totalValue = pflow.getTotalValue();
            Float incrValue = 0f;
            EcoPowerHourFlow powerHourFlow = new EcoPowerHourFlow(null, unitNo, null,
                    dateBetween, Integer.parseInt(dateBetween.format(DateTimeFormatter.ofPattern("yyyyMM"))), totalValue, totalValue, incrValue,
                    "system", LocalDateTime.now(), null, null);
            ecoPowerHourFlowMapper.insert(powerHourFlow);
        }
    }




    @Override
    public void initCoolHourFlowData() {
        //1.初始化制冷量
        /*EcoCoolHourFlow coolHourFlow = new EcoCoolHourFlow(null, null, null,
                LocalDateTime.of(2024, 5, 19, 0, 0), Integer.parseInt(LocalDateTime.of(2024, 5, 19, 0, 0).format(DateTimeFormatter.ofPattern("yyyyMM"))), 0f, 0f, 0f,0f,
                "system", LocalDateTime.now(), null, null);
        this.baseMapper.insert(coolHourFlow);*/

        //2.5月份的用电量和制冷量
        /*List<LocalDateTime> dateBetween1 = DateUtil.getDateBetween(LocalDateTime.of(2024, 5, 20, 0, 0), LocalDateTime.of(2024, 11, 29, 21, 0));
        dateBetween1.forEach(item ->{
            //制冷量
            EcoCoolHourFlow cflow = this.baseMapper.selectOne(Wrappers.<EcoCoolHourFlow>lambdaQuery().orderByDesc(EcoCoolHourFlow::getTimeValue).last("limit 1"));
            Float totalVal = cflow.getTotalValue();
            //获取该小时的用电量
            Map<String, Double> incrMap = ecoPowerHourFlowMapper.sumIncrByTimeValue(item);
            //获取cop
            Float cop = 0.0f;
            if (item.getMonth().name().equals("MAY") || item.getMonth().name().equals("JUNE") || item.getMonth().name().equals("JULY")) {
                cop = RandomUtil.getFloatRandom2(3.8f, 4.2f);
            }else if (item.getMonth().name().equals("AUGUST") || item.getMonth().name().equals("SEPTEMBER") || item.getMonth().name().equals("OCTOBER")){
                cop = RandomUtil.getFloatRandom2(4.2f, 4.6f);
            }
            Float powerIncrVal = Float.valueOf(String.format("%.2f", incrMap.get("incrValue")));
            Float incrVal = powerIncrVal * cop;
            totalVal += incrVal;
            EcoCoolHourFlow ecoCoolHourFlow = new EcoCoolHourFlow(null, null, null,
                    item, Integer.parseInt(item.format(DateTimeFormatter.ofPattern("yyyyMM"))), powerIncrVal, totalVal, incrVal, cop,
                    "system", LocalDateTime.now(), null, null);
            this.baseMapper.insert(ecoCoolHourFlow);
        });*/
    }

    public List<Map<String, Object>> getCopAnalysisList(String startDate, String endDate) {
        List<Map<String, Object>> list = new ArrayList<>();
        if (startDate.matches("\\d{4}-\\d{2}-\\d{2}") && endDate.matches("\\d{4}-\\d{2}-\\d{2}")) {
            list = this.baseMapper.copAnalysisListByDay(startDate + " 00", endDate + " 01");
        }
        if (startDate.matches("^\\d{4}-(0[1-9]|1[0-2])$") && endDate.matches("^\\d{4}-(0[1-9]|1[0-2])$")) {
            list = this.baseMapper.copAnalysisListByMonth(startDate.replace("-", ""), endDate.replace("-", ""));
        }
        return list;
    }

}
